﻿using System;
using System.Collections.Specialized;
using System.Configuration;

namespace ASPItalia.ModelVirtualCasting
{
    public static class Configuration
    {
        private const bool EXCEPTION_LOG_ENABLED = false;
        private const string REPOSITORY_ASSEMBLY_NAME = "ASPItalia.ModelVirtualCasting.Fake";
        private const string EMAIL_MESSAGE_SENDER = "noreply@domain.tld";
        private const string BASE_IMAGE_URL = "~/Images/{0}";

        private static readonly ConnectionStringSettingsCollection _connectionStrings = ConfigurationManager.ConnectionStrings;
        private static readonly NameValueCollection _appSettings = ConfigurationManager.AppSettings;

        internal static ConnectionStringSettingsCollection ConnectionStrings
        {
            get { return _connectionStrings; }
        }

        internal static NameValueCollection AppSettings
        {
            get { return _appSettings; }
        }

        internal static object UnitySection
        {
            get { return ConfigurationManager.GetSection(ConfigurationKeys.Unity); }
        }

        public static string ConnectionString
        {
            get { return Configuration.ConnectionStrings[ConfigurationKeys.ConnectionString].ConnectionString; }
        }

        public static bool ExceptionLogEnabled
        {
            get
            {
                bool isEnabled = EXCEPTION_LOG_ENABLED;
                if ((Configuration.AppSettings.IsNotNull()) && (!string.IsNullOrWhiteSpace(Configuration.AppSettings[ConfigurationKeys.ExceptionLogEnabled])))
                    if (bool.TryParse(AppSettings[ConfigurationKeys.ExceptionLogEnabled], out isEnabled))
                        return isEnabled;
                return EXCEPTION_LOG_ENABLED;
            }
        }

        public static RepositoryFactoryType RepositoryFactoryType
        {
            get
            {
                if ((Configuration.AppSettings.IsNotNull()) && (!string.IsNullOrWhiteSpace(Configuration.AppSettings[ConfigurationKeys.RepositoryFactoryType])))
                {
                    ModelVirtualCasting.RepositoryFactoryType factoryType;
                    string value = Configuration.AppSettings[ConfigurationKeys.RepositoryFactoryType];
                    
                    if (Enum.TryParse<RepositoryFactoryType>(value, out factoryType))
                    {
                        return factoryType;
                    }
                }

                return ModelVirtualCasting.RepositoryFactoryType.Default;
            }
        }

        public static string RepositoryAssemblyName
        {
            get
            {
                if ((Configuration.AppSettings.IsNotNull()) && (!string.IsNullOrWhiteSpace(Configuration.AppSettings[ConfigurationKeys.RepositoryAssemblyName])))
                    return Configuration.AppSettings[ConfigurationKeys.RepositoryAssemblyName];
                return REPOSITORY_ASSEMBLY_NAME;
            }
        }

        public static string EmailMessageSender
        {
            get
            {
                if ((Configuration.AppSettings.IsNotNull()) && (!string.IsNullOrWhiteSpace(Configuration.AppSettings[ConfigurationKeys.EmailMessageSender])))
                    return Configuration.AppSettings[ConfigurationKeys.EmailMessageSender];
                return EMAIL_MESSAGE_SENDER;
            }
        }

        public static string BaseImageUrl
        {
            get
            {
                if ((Configuration.AppSettings.IsNotNull()) && (!string.IsNullOrWhiteSpace(Configuration.AppSettings[ConfigurationKeys.BaseImageUrl])))
                    return Configuration.AppSettings[ConfigurationKeys.BaseImageUrl];
                return BASE_IMAGE_URL;
            }
        }
    }
}
